package com.cs.study.homework._0315;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;


public class Demo5 {
    public static void main(String[] args) {
        List<Student> students = generateStus();
        System.out.println(students);

        Map<Grade, List<Student>> map = new HashMap<>();

        for (Student student : students) {
            Grade grade = getGrade(student);
            // 从map中获取一些此时这个层级有哪些学生
            List<Student> stu = map.getOrDefault(grade, new ArrayList<Student>());
            // 把该学生加入到stu中
            stu.add(student);
            //更新map
            map.put(grade,stu);
        }
        // 遍历map
        Set<Map.Entry<Grade,List<Student>>> entries =map.entrySet();
        for (Map.Entry<Grade, List<Student>> entry : entries) {
            System.out.println(entry.getKey()+"=====>"+entry.getValue());
        }


    }

    private static Grade getGrade(Student stu) {
        int score = stu.getScore();
        Grade grade = null;
        if (score > 0 && score < 60) {
            grade = Grade.BAD;
        } else if (score >= 60 && score < 80) {
            grade = Grade.GOOD;
        } else {
            grade = Grade.EXCELLENT;
        }
        return grade;
    }

    private static List<Student> generateStus() {
        List<Student> students = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            Student stu = new Student();
            stu.setName("student-" + i);
            stu.setScore(random.nextInt(101));
            students.add(stu);
        }
        return students;
    }
}

enum Grade {
    BAD,
    GOOD,
    EXCELLENT
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Student {
    String name;
    int score;
}
